package com.tongfusoft.document.web.controller.administration;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.document.doc.service.IDocService;
import com.tongfusoft.document.dto.TreeDto;
import com.tongfusoft.document.entity.Docs;
import com.tongfusoft.document.entity.Folders;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.folders.service.IFoldersService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.ResultModel;
import com.tongfusoft.utils.JsonUtils;
import com.tongfusoft.utils.UIDGeneratorService;


@Controller
public class FoldersController extends BaseController{

	@Autowired
	@Qualifier(value="foldersService")
	public IFoldersService iFoldersService;
	
	@Autowired
	public IDocService docService;
	
	@RequestMapping("/manager/folders")
	@UrlDesc(value = "文件夹管理页")
	public String template(HttpServletRequest request, Model model) {

		model.addAttribute("mainPageKey", "folders");
		model.addAttribute("mainPage", "manager/folders/folders");

		return "base";
	}
	
	@RequestMapping("/folders/list")
	@UrlDesc(value = "文件夹查询")
	@ResponseBody
	public ResultModel menu(Model model){
		ResultModel rm = new ResultModel<List>();
		
		rm.setState("success");
		try{
			Folders folders = new Folders();
			
			TreeDto treeDtosList = iFoldersService.foldersList(folders);
			//logger.debug(JsonUtils.getJsonString4JavaPOJO(treeDtosList));
			
			rm.setData(treeDtosList);
		}catch(Exception e){
			
			e.printStackTrace();
			rm.setState("error");
		}
		return rm;
	}
	
	@RequestMapping("/folders/update")
	@UrlDesc(value = "更新文件夹")
	@ResponseBody
	public ResultModel update(Folders folders) {

		ResultModel rm = new ResultModel();
		
		try {
			this.iFoldersService.updateFolders(folders,(Users)super.getUser());
			rm.setState("success");
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}
	
	@RequestMapping("/folders/create")
	@UrlDesc(value = "创建文件夹")
	@ResponseBody
	public ResultModel add(Folders folders) {
		ResultModel rm = new ResultModel();
		try {
			if(folders.getFolderName()==null || folders.getFolderName()==""){
				rm.setState("errornull");
			}else{
				this.iFoldersService.createFolders(folders, (Users)super.getUser());
				rm.setState("success");
			}
		} catch(ServiceException se){
			
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}
	
	@RequestMapping("/folders/del")
	@UrlDesc(value = "删除文件夹")
	@ResponseBody
	public ResultModel del(Folders folders) {
		ResultModel rm = new ResultModel();
		
		rm.setState("success");
		try {
			this.iFoldersService.deleteFolders(folders);
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}
	
	@RequestMapping("/folders/isHasDocument")
	@UrlDesc(value = "文件夹下是否有文档")
	@ResponseBody
	public ResultModel isDocument(Folders folders) {
		ResultModel rm = new ResultModel();
		
		try {
			Docs doc = new Docs();
			doc.setDocFolder(folders.getFolderId());
			
			int pageSize =10;
			int pageIndex =1;
			PageInfo pageInfo = this.docService.searchDoc(doc, (Users) super.getUser(), pageSize, pageIndex);
			int docNum = pageInfo.getSize();
			if(docNum > 0){
				rm.setState("success");
			}else{
				rm.setState("error");
			}
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}
	
	
}
